var item = (function() {
	var gid;
	var uname;
	var type;
	var uid;
	var ts;
	var txt;
	var me = this;
	
	var DBName = DB_NAME; //"ENA_DB";
	var itemTable = "notification";
	
	var itemObj = function() {
		return {
			"gid": me.gid,
			"type": me.type,
			"uname": me.uname,
			"uid": me.uid,
			"text": me.text,
			"timestamp": me.ts
		};
	};
	
	var parseLink = function(link) {
		var str = link;
		me.link = str;
		str = str.split('%22');
		if (str.length > 1) {
			str = str[3];
			str = base64.decode(str);
			while (String(str.substr(0, str.length - 1)).lastIndexOf('}') != -1) { str = str.substr(0, str.length - 1) }
			var linkObj = JSON.parse(str);
			me.gid = (linkObj.gift) ? linkObj.gift : (linkObj.buildingName) ? linkObj.buildingName : undefined;
			me.type = (linkObj.gift) ? itemType.gift : (linkObj.buildingName) ? itemType.crew : itemType.request;
			me.ts = (linkObj.timestamp) ? linkObj.timestamp : undefined;
			me.uid = (linkObj.signature) ? linkObj.signature : undefined;
		}
	};
	
	var itemType = (function() { 
		return {crew: 0, gift: 1, request: 2, coins: 3, reward: 4}; 
	})();
	
	var load = function() {
		return {
			"loadFrom": (function() {
				return {
					"html": function(htmlText) {
						var div = document.createElement('div');
						div.innerHTML = htmlText;	//load html
						
						parseLink('https://plus.google.com/u/0/' + $('span.LFb > a', div).attr('href'));
						me.uname = $('span.HFb > a', div).text();
						me.text = $('div.IFb > div', div).text();
						
						return $('div.E7a', div).attr('link_id') 
					},
					"db": function(obj) {
						me.gid = obj.gid;
						me.type = obj.type;
						me.ts = obj.ts;
						me.uname = obj.name;
						me.text = obj.text;
						me.uid = obj.uid;
						return itemObj();
					}
				};
			}())
		}
	}
	
	return {
		"load": load,
		"save": function(item) {
			if (item.uid) {
				$.indexedDB(DBName)
				.objectStore(itemTable)
				.add({ 
					gid: item.gid, 
					type: item.type, 
					uname: item.uname, 
					text: item.text, 
					timestamp: item.timestamp, 
					uid: item.uid
				});
			}
		},
		"delete": function(callback) {
			if (me.uid) {
				$.indexedDB(DBName).objectStore(itemTable)["delete"](me.uid).done(function() {
					if (typeof callback === 'function') {
						callback();
					}
				});
			}
		},
		"create": function(link, uname, text) {
			me.uname = uname;
			me.text = text;
			
			parseLink(link);
			
			return itemObj;
		},
		"get": function(key) {
			$.indexedDB(DBName).objectStore(itemTable).get(key).done(function(val) { 
				return load.loadFrom.db(val);
			});
		},
		"print": function(item) {
			if (item.uid) {
				return JSON.stringify(item);
			}
		},
		"type": itemType
	};
})();